package me.obstsalat.guildera;

import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent;

public class d_protection {
	private static guildera plugin;
	
	d_protection(guildera pl) {
		plugin = pl;
	}
	
	public boolean onBlockPlace(BlockPlaceEvent event) {
		Player ply = event.getPlayer();
		Block bl = event.getBlockPlaced();
		if(!plugin.checkViableWorld(ply.getWorld())) {
			return false;
		}
		
		if(!ply.isOp())  {
			if(plugin.overworld.getName().equals(ply.getWorld().getName())) {
				for(Location2d l : plugin.rA) {
					if(l.equal(new Location2d(bl.getX(), bl.getZ()))) {
						event.setBuild(false);
						if(ply != null) {
							ply.sendMessage("You can't build in this area.");
						}
					}
				}
			}
		}
		return !event.canBuild();
	}
	
	public boolean onEntityExplode(EntityExplodeEvent event) {
		if(!plugin.checkViableWorld(event.getEntity().getWorld())) {
			return false;
		}
		if(!plugin.overworld.getName().equals(event.getEntity().getWorld().getName())) {
			return false;
		}
		boolean evil = false;
		for(int i = 0; i < event.blockList().size(); i++) {
			Block bl = event.blockList().get(i);
			for(Location2d l : plugin.rA) {
				if(l.equal(new Location2d(bl.getX(), bl.getZ()))) {
					evil = true;
				}
			}
		}
		event.setCancelled(evil);
		return evil;
	}

	public boolean onBlockBreak(BlockBreakEvent event) {
		Block bl = event.getBlock();
		if(!plugin.checkViableWorld(bl.getWorld())) {
			return false;
		}
		if(!plugin.overworld.getName().equals(event.getBlock().getWorld().getName())) {
			return false;
		}
		if(!event.getPlayer().isOp()) {
			for(Location2d l : plugin.rA) {
				if(l.equal(new Location2d(bl.getX(), bl.getZ()))) {
					event.setCancelled(true);
					if(event.getPlayer() != null) {
						event.getPlayer().sendMessage("You can't break blocks in this area.");
					}
				}
			}
		}
		return event.isCancelled();
	}
}
